package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.MessageEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.friend.FriendInformUI;

/**
 * 
 * 医生控制台单聊
 * @author zhao
 * 
 */
public class DoctorStationFriendListAdapter extends BaseAdapter {
	public final List<CustomerInfoEntity> mInfoEntities = new ArrayList<CustomerInfoEntity>();
	final LayoutInflater mInflater;
	ImageLoader mImageLoader;
	final Activity mActivity;
	
	public DoctorStationFriendListAdapter(List<CustomerInfoEntity> list,LayoutInflater inflater,Activity context) {
		this.mInflater = inflater;
		this.mActivity = context;
		mInfoEntities.addAll(list);
		mImageLoader = mImageLoader.getInstance();
	}
	
	@Override
	public int getCount() {
		return mInfoEntities.size();
	}

	@Override
	public CustomerInfoEntity getItem(int position) {
		return mInfoEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		ConcurrentHashMap<String, List<MessageEntity>> messageMap = HTalkApplication.getAppData().messageCllection;
		final CustomerInfoEntity customerInfoEntity = getItem(position);
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.friendlist_item1,null);
			viewHolder.headerImageV = (ImageView)convertView.findViewById(R.id.head_image);
			viewHolder.mesgTextV = (TextView)convertView.findViewById(R.id.item_dele);
			viewHolder.nameTextV = (TextView)convertView.findViewById(R.id.name);
			viewHolder.sexImageV = (ImageView)convertView.findViewById(R.id.head_sex);
			viewHolder.levelImageV = (ImageView)convertView.findViewById(R.id.levl);
			viewHolder.noteTextV = (TextView)convertView.findViewById(R.id.note);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		if (messageMap.containsKey(customerInfoEntity.getId()) && messageMap.get(customerInfoEntity.getId()).size() != 0) {
			viewHolder.mesgTextV.setText(messageMap.get(customerInfoEntity.getId()).size()+"");
			viewHolder.mesgTextV.setVisibility(View.VISIBLE);
		}else {
			viewHolder.mesgTextV.setVisibility(View.GONE);
		}
		
		
		viewHolder.noteTextV.setText(customerInfoEntity.getDescription());
		String iconPath = customerInfoEntity.getNormalHeadIcon();
		mImageLoader.displayImage(customerInfoEntity.getSex(), iconPath,viewHolder.headerImageV);
		String sex = customerInfoEntity.getSex();
		if("1".equals(sex)){
			viewHolder.sexImageV.setImageResource(R.drawable.man);
		}else if("2".equals(sex)){
			viewHolder.sexImageV.setImageResource(R.drawable.women);
		}else{
			viewHolder.sexImageV.setImageResource(0);
		}
		viewHolder.nameTextV.setText(customerInfoEntity.getName());
		viewHolder.headerImageV.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//头像点击跳转资料界面
				Intent intent = new Intent(mActivity,FriendInformUI.class);
				intent.putExtra("id",customerInfoEntity.getId());
				mActivity.startActivity(intent);
				mActivity.overridePendingTransition(R.anim.anim_enter,R.anim.anim_exit);
			}
		});
		return convertView;
	}
	
	static class ViewHolder{
		ImageView headerImageV;
		TextView mesgTextV;
		TextView nameTextV;
		ImageView sexImageV;
		ImageView levelImageV;
		ImageView monyImageV;
		TextView noteTextV;
	}
}
